//
//  DHSortView.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/4/28.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHSortView.h"
#define kScreenWidth UIScreen.mainScreen.bounds.size.width
#define kScreenHeight UIScreen.mainScreen.bounds.size.height
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

@interface DHSortView()
@property (weak, nonatomic) IBOutlet UIButton *intergSort;
@property (weak, nonatomic) IBOutlet UIButton *salesSort;
@property (weak, nonatomic) IBOutlet UIButton *filteView;

@property(nonatomic ,strong)UIButton *currentSortoderBtn; // 记录当前排序按钮

@property(nonatomic ,assign,getter=isGrid)BOOL grid;


@end

@implementation DHSortView


#pragma mark: - 排序按钮的监听
- (IBAction)sortWayButtonClick:(UIButton *)sender {
    NSAssert(self.sortoderListGoodBlock != nil, @"sortoderListGoodBlock不能为nil");
    
    // 判断当前选中的按钮 调用闭包
    switch (sender.tag) {
        case ListSortorderDefault:
            
           
            sender.selected = YES;
            self.currentSortoderBtn.selected = NO;
            self.oder = ListorderNone;
            self.sortKey = ListSortorderDefault;

            self.sortoderListGoodBlock(ListSortorderDefault);
            [self selectedSortMethod];
      
            break;
        case ListSortorderHightToLowPrice:
            
        
            sender.selected = YES;
            self.currentSortoderBtn.selected = NO;
            self.oder = ListorderDec;
            self.sortKey = ListSortorderLowToHightPrice;
            self.sortoderListGoodBlock(ListSortorderHightToLowPrice);
            [self selectedSortMethod];

            break;
        case ListSortorderLowToHightPrice:
          
            sender.selected = YES;
            self.currentSortoderBtn.selected = NO;
            self.oder = ListorderAsc;
            self.sortKey = ListSortorderLowToHightPrice;
            self.sortoderListGoodBlock(ListSortorderLowToHightPrice);

            [self selectedSortMethod];

            break;
        case ListSortorderPopWay:
            sender.selected = YES;
            self.currentSortoderBtn.selected = NO;
            self.oder = ListorderNone;
            self.sortKey = ListSortorderNone;
            
            self.sortoderListGoodBlock(ListSortorderPopWay);
            [self selectedSortMethod];

           
            break;
    }
    // 记录当前选中按钮
    self.currentSortoderBtn = sender;
}
// 筛选按钮监听
- (IBAction)filterButtonCilck:(UIButton *)sender {
    
    NSAssert(self.showFilterView != nil, @"self.showFilterView 不能为nil");
    
    self.showFilterView();
}

#pragma mark: - 界面变化的按钮监听
- (IBAction)changeViewModeClick:(UIButton *)sender {
    
    
    sender.selected = !sender.selected;
    NSAssert(self.viewPresentMode != nil, @"self.viewPresentMode 不能为nil");
    self.grid = !self.grid;
    self.viewPresentMode(self.grid);
}


/// 从xib加载排序View
+ (instancetype)sortView{
    
    UINib *nib = [UINib nibWithNibName:@"DHSortView" bundle:nil];
    
    DHSortView *sortView = [nib instantiateWithOwner:nil options:nil][0];
    
    sortView.frame = CGRectMake(0, 0, kScreenWidth, 34);
    return sortView;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self.intergSort addTarget:self action:@selector(selectedSortMethod) forControlEvents:UIControlEventTouchUpInside];
   
    self.intergSort.selected = YES;
    
    //销量排序的按钮注册监听
    [self.salesSort addTarget:self action:@selector(saleSortButtonCilcik) forControlEvents:UIControlEventTouchUpInside];
   
    self.clipsToBounds = YES;
    
    self.grid = NO;
}
#pragma mark: - 按销量排序的按钮
- (void)saleSortButtonCilcik{
    self.intergSort.selected = NO;
    self.salesSort.selected = YES;
    //self.currentSortoderBtn.selected = NO;
    self.oder = ListorderNone;
    self.sortKey = ListSortorderNone;
    
    self.sortoderListGoodBlock(ListSortorderPopWay);
}


#pragma mark: - interSort 监听
- (void)selectedSortMethod{
   // self.selectedSortMothodShow(self.selectedView);
    self.intergSort.selected = YES;
    self.salesSort.selected = NO;

    CGFloat height = (self.bounds.size.height == 34) ? self.bounds.size.height * 4 + 15 : 34;
    CGFloat y = KIsiPhoneX ? 88 : 64;
    
    CGRect frame = self.frame;
    
    frame = CGRectMake(0, y, kScreenWidth,height);
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.frame = frame;
        
        [self layoutIfNeeded];

    }];
}

@end
